<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>数据库权限管理</title>
<link href="../css/css.css" rel="stylesheet" type="text/css" />
</head>
<body>
<script type="text/javascript" src="/javascript/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="/javascript/jquery-ui.min.js"></script>
<script type="text/javascript" src="/javascript/jquery.multiselect.js"></script>
<link type="text/css" href="/css/jquery.multiselect.css" rel="stylesheet" />
<link type="text/css" href="/css/jquery-ui.css" rel="stylesheet" />
<script>
	function randomString(len){
		len = len || 32;
		var chars= "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
		var maxPos=chars.length;
		var pwd='';
		for (i = 0; i < len; i++) {
			pwd += chars.charAt(Math.floor(Math.random()*maxPos));
		}
		return pwd;
	};
	
	$(function(){
	   $("#tables").multiselect({
	       noneSelectedText: "全部",
	       checkAll: function(){},
           uncheckAll: function(){
                var optStr = $("input[name='multiselect_tables']:checkbox").map(function(){   
                            return $(this).val();         
                            }).get().join(",");
                $("#tablename").val(optStr);
            },
            selectedText: function(numChecked, numTotal, checkedItems){
                var idcs=checkedItems.map(function(element){ return element.value; }).join(',');
                $("#tablename").val(idcs);
                if(numChecked==numTotal){
                    return "全部";
                }
            return (numChecked <= 1) ? idcs : numChecked + ' of ' + numTotal + ' checked';
            }
        });
        $("#hostname").multiselect({
            multiple: false,
            header: false,
            noneSelectedText: "*",
            selectedList: 1
        });
        $("#dbname").multiselect({
            multiple: false,
            header: false,
            noneSelectedText: "全部",
            selectedList: 1
        });
        
		if($("#massage").val().length > 0)
			alert($("#massage").val());
		$("[name='priviledge']").click(function(){
		    var privs="";
			$("[name='priviledge']").each(function(){
		 		var val=$(this).attr("value");
		 		if($(this).attr("checked"))
		 			privs+=","+val;
		 	});
		 	$("#priv").val(privs);
		});
		
		$("#add,#revoke").click(function(){
			var pwd=$("#password").val();
			if(pwd==""){
				alert("请生成密码！");
				return false;
			}
			var priv=$("#priv").val();
			if(priv==""){
			 alert("请选择权限！");
			 return false;
			}
			if($(this).attr("name")=="revoke"){
			      $("#addPriv").attr("action","/dbmanager/del");
			}
			$("#addPriv").submit();
		});
		$("#search").click(function(){
		    var pwd=$("#password").val();
            if(pwd==""){
                alert("尚未添加权限！");
                return false;
            }          
            $("#addPriv").attr("action","/dbmanager/show");
            $("#addPriv").submit();
		});
		$("#getpwd").click(function(){
			var pwd=randomString(15);
			$("#password").val(pwd);
		});
	});
	
	$(function(){
        $("#searchHost,#searchDBName,#searchTname").multiselect({
            multiple: false,
            header: false,
            noneSelectedText: "全部",
            selectedList: 1
        });
    });
    
	
 $(function(){
   $("#hostname").change(function(){
      var hostname = $("#hostname").val();
      if(hostname != "" && hostname != "*"){
        jQuery.ajax({
        type:"GET",
        dataType:"json",
        async:true,
        url:"/dbtools/displaydb",
        data:{"hostname":hostname},
        success:function(data){
           var dbNames ="<option value='*'>全部</option> ";
           if(data!="" && data.result =="right"){
              $.each(data.dbNameList, function(i, obj) {
                 dbNames = dbNames + "<option value='"+obj+"'>"+obj+"</option>";
              });
             
           }else{
              alert("error!");
           }
           $("#dbname").empty().append(dbNames);
           $("#dbname").multiselect('refresh');
        }
      });
      }else{
          $("#dbname").empty();
          $("#dbname").multiselect('refresh');
      }
      $("#tables").empty();
      $("#tables").multiselect('refresh');
      $("#tablename").val("*");
    });
});


 $(function(){
   $("#dbname").change(function(){
      var hostname = $("#hostname").val();
      var dbname = $("#dbname").val();
      if(hostname != "*" && dbname !="*"){
        jQuery.ajax({
        type:"GET",
        dataType:"json",
        async:true,
        url:"/dbtools/displayTable",
        data:{"hostname":hostname,"dbname":dbname},
        success:function(data){
            var tbNames = "";
            var newtablenames="";
           if(data!="" && data.result =="right"){
              $.each(data.tableNameList, function(i, obj) {
                 tbNames = tbNames + "<option value='"+obj+"'>"+obj+"</option>";
              });
              newtablenames = $.map(data.tableNameList, function (item,i) { return item; }).join(',');
           }
          $("#tables").empty().append(tbNames);
          $("#tables").multiselect('refresh');
          $("#tablename").val(newtablenames);
        }
      });
      }else{
          var cateList = "";
          $("#tables").empty().append(cateList);
          $("#tables").multiselect('refresh');
          $("#tablename").val("*");
      }
    });
});


 $(function(){
   $("#searchHost").change(function(){
      var searchHost = $("#searchHost").val();
      if(hostname != ""){
        jQuery.ajax({
        type:"GET",
        dataType:"json",
        async:true,
        url:"/dbtools/displaydb",
        data:{"hostname":searchHost},
        success:function(data){
            var dbNames="<option value=''>全部</option> ";
           if(data!="" && data.result =="right"){
             var dbNames="<option value=''>全部</option> ";
              $.each(data.dbNameList, function(i, obj) {
                 dbNames = dbNames + "<option value='"+obj+"'>"+obj+"</option>";
              });
              
           }else{
              alert("error!");
           }
           $("#searchDBName").empty().append(dbNames);
           $("#searchDBName").multiselect('refresh');
        }
      });
      }else{
          $("#searchDBName").empty().append("<option value=''>全部</option>");
          $("#searchDBName").multiselect('refresh');
      }
      $("#searchTname").empty().append("<option value=''>全部</option>");
      $("#searchTname").multiselect('refresh');
    });
});


 $(function(){
   $("#searchDBName").change(function(){
      var searchHost = $("#searchHost").val();
      var searchDBName = $("#searchDBName").val();
      if(hostname != "*" && dbname !="*"){
        jQuery.ajax({
        type:"GET",
        dataType:"json",
        async:true,
        url:"/dbtools/displayTable",
        data:{"hostname":searchHost,"dbname":searchDBName},
        success:function(data){
            var tbNames = "<option value=''>全部</option>";
           if(data!="" && data.result =="right"){
              $.each(data.tableNameList, function(i, obj) {
                 tbNames = tbNames + "<option value='"+obj+"'>"+obj+"</option>";
              });
           }
          $("#searchTname").empty().append(tbNames);
          $("#searchTname").multiselect('refresh');
        }
      });
      }else{
          $("#searchTname").empty().append("<option value=''>全部</option>");
          $("#searchTname").multiselect('refresh');
      }
    });
});

</script>
<div class="lr_both">
<div class="right_conts">
  <div class="mg_tt">
    <div class="right_search">
      <div class="rs_title_bg">
        <h2 class="rs_title">数据库权限管理</h2>
      </div>
	  </br>
	  <form id="addPriv" name="addPriv" action="/dbmanager/add" method="post">
	   <table class="sj_tl"  width="100%" border="0" cellspacing="0" cellpadding="0">
		<tr>
          <th align="center" colspan="2">用户基本信息</th>
		</tr>
		
        <tr>
           <td align="center" >用户名称</td>
		  <td align="center"  ><input type="text"  readonly="readonly" name="username" id="username" value="$!{username}"/></td>
       		<td align="left" style="border:none">
       			<input type="hidden" name="hasUser" id="hasUser" value="$!{hasUser}"/>
       			<input type="hidden" name="massage" id="massage" value="$!{massage}"/>
       		</td>
        </tr>
        
		 <tr>
		 	<td align="center" >密码</td>
		  	<td align="center" ><input type="text" readonly="readonly" name="password" id="password" value="$!{password}"/></td>
		  	#if(!$hasUser)
		    	<td align="left" style="border:none"><input type="button"  name="getpwd" id="getpwd" value="生成密码"/></td>
		    #end
        </tr>
				
	 </table>
	 
      <div style="background:#e3eba6;padding-top:10px;padding-left: 22px;padding-bottom:10px">
		<div class="pd_text">主机:</div>
        <div class="input_div">
          <select class="form-control"  size="1"  name="hostname" id="hostname">
           <option value="*">全部</option>
			#foreach($host in $dbHostList)
			<option value="${host.hostname}">${host.hostname}</option>
			#end
          </select>
        </div>
        <div class="pd_text">数据库:</div>
        <div class="input_div">
        	<select  class="form-control"  size="1"  name="dbname" id="dbname">
        	<option value="*">全部</option>
        	</select>
        </div>
		 <div class="pd_text">数据表:<input type="hidden" name="tablename" id="tablename" value="*" /></div>
        <div class="input_div">
          <select name="tables" id="tables" multiple="multiple">
          </select>
        </div>
		&nbsp;&nbsp;&nbsp;&nbsp;
		<input type="submit" name="add" id="add" value="添加"/>&nbsp;&nbsp;&nbsp;&nbsp;
		<input type="submit" name="revoke" id="revoke" value="回收"/>
		</div>
      
        <div style="background:#e3eba6;padding-top:5px;padding-left: 30px;padding-bottom:10px">
         <label>权限:</label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
     	<input type="hidden" name="priv" id="priv" value=""/>
      	<input type="checkbox" name="priviledge" id="select" value="select" onclick="">select</input>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
      	<input type="checkbox" name="priviledge" id="insert" value="insert" onclick="">insert</input>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
      	<input type="checkbox" name="priviledge" id="update" value="update" onclick="">update</input>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
      	<input type="checkbox" name="priviledge" id="delete" value="delete" onclick="">delete</input>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
      	<input type="checkbox" name="priviledge" id="drop" value="drop" onclick="">drop</input>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
      </div>
    
      <div style="padding-top:15px;padding-left: 22px;padding-bottom:5px">
        <div class="pd_text">主机:</div>
        <div class="input_div">
          <select class="form-control"  size="1"  name="searchHost" id="searchHost">
           <option value="">全部</option>
            #foreach($host in $dbHostList)
            <option value="${host.hostname}">${host.hostname}</option>
            #end
          </select>
        </div>
        <div class="pd_text">数据库:</div>
        <div class="input_div">
            <select  class="form-control"  size="1"  name="searchDBName" id="searchDBName">
                <option value="">全部</option>           
            </select>
        </div>
         <div class="pd_text">数据表:</div>
        <div class="input_div">
          <select name="searchTname" id="searchTname">
            <option value="">全部</option>        
          </select>
        </div>
        &nbsp;&nbsp;&nbsp;&nbsp;
        <input type="submit" name="search" id="search" value="查找"/>
       </div>
        </form>
    </div>
    
    <div class="table_div">
      <table class="sj_tl sj_tl_2" width="100%" border="0" cellspacing="0" cellpadding="0">
        <tr>
          <th align="center" colspan="6">数据库权限列表</th>
        </tr>
		<tr style="background:#ffd9b5;">
		   <td align="center" >用户名称</td>
		   <td align="center" >主机</td>
		   <td align="center" >数据库</td>
		   <td align="center" >数据表</td>
		   <td align="center" >操作权限</td>
		   <td align="center" >操作</td>
        </tr>
        #set($dbPriList = $privList.items)
        #foreach($dbPri in $dbPriList)
		<tr onMouseOver="this.className='tdbg10'" onMouseOut="this.className='tdbg11'" class="tdbg11">
           <td align="center" >$dbPri.username</td>
		   <td align="center" >$dbPri.hostname</td>
           <td align="center" >$dbPri.dbname</td>
    	   <td align="center" >$dbPri.tablename</td>
   		   <td align="center" >$!dbPri.priviledge</td>
           <td align="center" >
                <a href="/dbmanager/toedit?username=$dbPri.username&hostname=$dbPri.hostname&dbname=$dbPri.dbname&tablename=$dbPri.tablename&priv=$!dbPri.priviledge">编辑</a> &nbsp &nbsp 
			    <a href="/dbmanager/del?username=$dbPri.username&hostname=$dbPri.hostname&dbname=$dbPri.dbname&tablename=$dbPri.tablename&priv=$!dbPri.priviledge" onclick="if(confirm('确定要删除吗?')){return true;} return false;">删除</a>
			</td>
        </tr>
		#end
      </table>
	  </br>
       <div align="right">
        #parse("/views/default/page.vm")
        #pagebar($privList "/dbmanager/show?#page#&$paramurl")
       </div>
    </div>


  </div>
</div>
</div>
</body>
</html>
